/**
 * Copyright 2011 rkehoe
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 */
package example.domain.objects;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import queryable.store.annotations.Ignore;
import queryable.store.annotations.Index;
import queryable.store.annotations.Indexes;



@Entity
@Indexes(
		{@Index(name="MAN_COL_INDEX", columnNames={"manufacturer,Colour"}),
		@Index(name="COL_INDEX", columnNames={"Colour"})
		})
@Table(name="vehicle")
public class Vehicle implements Serializable
{
	private static int COUNT = 0;
	private static final long serialVersionUID = 1L;
	
	public String	colour;

	@Column()
	public String	manufacturer;
	@Column()
	public String	model;
	@Column()
	public String	type;
	@Column()
	@Ignore
	public double	weight;
	@Column()
	public int	  seats;
	@Column()
	public int	  doors;
	@Column()
	public int	  engineCapacity;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	private int count;

	/**
     * 
     */
    public Vehicle()
    {
    }
    
    public Vehicle(Colour colour, Manufacturer manufacturer, VehicleType type, String model, double weight, int seats, int doors, int engineCapacity)
    {
    	this.count = COUNT++;
    	this.id = new Long(count);
    	this.model = model;
		this.weight = weight;
		this.seats = seats;
		this.doors = doors;
		this.engineCapacity = engineCapacity;
		this.colour = colour.name();
    	this.manufacturer = manufacturer.name();
    	this.type = type.name();
    }
    
    @Override
    public String toString()
    {
	    return "Vehicle [colour=" + this.colour + ", manufacturer=" + this.manufacturer + ", model=" + this.model + ", type=" + this.type + ", weight=" + this.weight + ", seats="
	            + this.seats + ", doors=" + this.doors + ", engineCapacity=" + this.engineCapacity + ", id=" + this.id + ", count=" + this.count + "]";
    }



	public Vehicle setColour(Colour c)
    {
	    this.colour = c.toString();
	    return this;
    }

	/**
     * @return the id
     */
    public Long getId()
    {
	    return this.id;
    }
    
    /**
     * @return the colour
     */
	@Column(name="Colour")
    public String getColour()
    {
	    return this.colour;
    }

	/**
     * @return the manufacturer
     */
    public String getManufacturer()
    {
	    return this.manufacturer;
    }
    
	@Override
    public int hashCode()
    {
	    final int prime = 31;
	    int result = 1;
	    result = prime * result + ((this.colour == null) ? 0 : this.colour.hashCode());
	    result = prime * result + this.count;
	    result = prime * result + this.doors;
	    result = prime * result + this.engineCapacity;
	    result = prime * result + ((this.manufacturer == null) ? 0 : this.manufacturer.hashCode());
	    result = prime * result + ((this.model == null) ? 0 : this.model.hashCode());
	    result = prime * result + this.seats;
	    result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
	    long temp;
	    temp = Double.doubleToLongBits(this.weight);
	    result = prime * result + (int) (temp ^ (temp >>> 32));
	    return result;
    }

	@Override
    public boolean equals(Object obj)
    {
	    if (this == obj)
		    return true;
	    if (obj == null)
		    return false;
	    if (getClass() != obj.getClass())
		    return false;
	    Vehicle other = (Vehicle) obj;
	    if (this.colour == null)
	    {
		    if (other.colour != null)
			    return false;
	    }
	    else if (!this.colour.equals(other.colour))
		    return false;
	    if (this.count != other.count)
		    return false;
	    if (this.doors != other.doors)
		    return false;
	    if (this.engineCapacity != other.engineCapacity)
		    return false;
	    if (this.manufacturer == null)
	    {
		    if (other.manufacturer != null)
			    return false;
	    }
	    else if (!this.manufacturer.equals(other.manufacturer))
		    return false;
	    if (this.model == null)
	    {
		    if (other.model != null)
			    return false;
	    }
	    else if (!this.model.equals(other.model))
		    return false;
	    if (this.seats != other.seats)
		    return false;
	    if (this.type == null)
	    {
		    if (other.type != null)
			    return false;
	    }
	    else if (!this.type.equals(other.type))
		    return false;
	    if (Double.doubleToLongBits(this.weight) != Double.doubleToLongBits(other.weight))
		    return false;
	    return true;
    }
	
}

